package com.zyx.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyx.mall.entity.Cart;
import com.zyx.mall.entity.queryObject.CartQuery;
import com.zyx.mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  购物车
 * </p>
 *
 * @author zyx
 * @since 2023-01-06
 */
@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    //添加购物车
    @PostMapping("/add")
    public Boolean addCart(@RequestBody Cart cart){
        System.out.println(cart.toString());
        //判断某用户的购物车是否添加过该商品
        String userId = cart.getUserId();
        String productId = cart.getProductId();
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("product_id",productId);
        Cart one = cartService.getOne(wrapper);
        //如果该用户没有该商品的购物车，直接加入
        if (one==null){
            cartService.save(cart);
        }else{
            //否则，就在原来的购物车数据上把数量加1
            cart.setProductQty(one.getProductQty()+1);
            cart.setId(one.getId());
            cartService.updateById(cart);
        }
        return true;
    }

    //删除购物车
    @DeleteMapping("/del")
    public Boolean delCart(String id){
        return cartService.removeById(id);
    }

    //删除购物车，多删
    @DeleteMapping("/del/ids")
    public Boolean delCart(String[] ids){
        return cartService.removeByIds(Arrays.asList(ids));
    }

    //更新购物车
    @PutMapping("/update")
    public Boolean updateCart(@RequestBody Cart cart){
        return cartService.updateById(cart);
    }

    //根据id查询购物车
    @GetMapping("/get")
    public Cart getCartById(String id){
        return cartService.getById(id);
    }

    //可带条件查询购物车
    @GetMapping("/list")
    public List<Cart> getList(CartQuery cartQuery){
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq(cartQuery.getUserId()!=null&&cartQuery.getUserId()!="","user_id",cartQuery.getUserId())
                .like(cartQuery.getProductName()!=null&&cartQuery.getProductName()!="","product_name",cartQuery.getProductName())
                .eq(cartQuery.getProductId()!=null&&cartQuery.getProductId()!="","product_id",cartQuery.getProductId())
                .in("id",cartQuery.getIds());
        List<Cart> list = cartService.list(wrapper);
        return list;
    }

    //分页查询购物车
    @GetMapping("/pageList")
    public Page pageList(CartQuery cartQuery,Integer currentPage,Integer pageSize){
        Page<Cart> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(cartQuery.getUserId()!=null&&cartQuery.getUserId()!="","user_id",cartQuery.getUserId())
                .like(cartQuery.getProductName()!=null&&cartQuery.getProductName()!="","product_name",cartQuery.getProductName())
                .eq(cartQuery.getProductId()!=null&&cartQuery.getProductId()!="","product_id",cartQuery.getProductId());
        cartService.page(page,queryWrapper);
        return page;
    }
}

